package com.jxdinfo.hussar.example.common.schedulingtask.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdUnitMeasureMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.dao.MdVendorMapper;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdUnitMeasure;
import com.jxdinfo.hussar.example.common.schedulingtask.model.MdVendor;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdUnitMeasureService;
import com.jxdinfo.hussar.example.common.schedulingtask.service.MdVendorService;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdUnitMeasureVO;
import com.jxdinfo.hussar.example.common.schedulingtask.vo.MdVendorVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 供应商表 服务实现类
 * </p>
 *
 * @author 乔镔
 * @since 2024-04-03
 */
@Service("com.jxdinfo.hussar.example.common.schedulingtask.service.impl.MdVendorServiceImpl")
public class MdVendorServiceImpl extends ServiceImpl<MdVendorMapper, MdVendor> implements MdVendorService {

    private static final Logger logger = LoggerFactory.getLogger(MdVendorServiceImpl.class);

    @Autowired
    private MdVendorService mdVendorService;

    @Autowired
    private MdVendorMapper mdVendorMapper;

    @Override
    public void manualSynchronization() {
        logger.info("\n手动同步-供应商表-开始同步供应商表");
        LocalDateTime nowTime = LocalDateTime.now();
        // 1查询erp所有的供应商表信息
        List<MdVendorVO> allMdVendors = mdVendorMapper.selectAllClient();
        // 2供应商表信息转换成主键-对象map集合
        Map<Long, MdVendorVO> allMdVendorVOMap = allMdVendors.stream().collect(Collectors.toMap(MdVendorVO::getVendorId, t -> t));
        // 3供应商表信息中的id转换成id列表
        List<Long> erpAllMdVendorIdList = allMdVendors.stream().map(MdVendorVO::getVendorId).collect(Collectors.toList());
        // 4查询所有的mes供应商表信息
        QueryWrapper<MdVendor> mdVendorWrapper = new QueryWrapper<>();
        mdVendorWrapper.eq("deleted_flag","0");
        List<MdVendor> mesMdVondors = mdVendorMapper.selectList(mdVendorWrapper);
        // 5mes中的客户信息转换成id列表,
        List<Long> mesIdList = mesMdVondors.stream().map(MdVendor::getVendorId).collect(Collectors.toList());
        // 6找出erp中有但是mes中没有的id，这写id需要插入
        ArrayList<Long> erpHasMesNoIdList = new ArrayList<>();
        for (int i = 0; i < erpAllMdVendorIdList.size(); i++) {
            if (!mesIdList.contains(erpAllMdVendorIdList.get(i))) {
                erpHasMesNoIdList.add(erpAllMdVendorIdList.get(i));
            }
        }
        if (erpHasMesNoIdList.size() > 0) {
            // 循环创建对象，保存待要插入的结果
            ArrayList<MdVendor> insertList = new ArrayList<>();
            for (Long insertId : erpHasMesNoIdList) {
                MdVendorVO mdVendorVO = allMdVendorVOMap.get(insertId);
                MdVendor mdVendor = new MdVendor();
                mdVendor.setVendorId(mdVendorVO.getVendorId());
                mdVendor.setVendorCode(mdVendorVO.getVendorCode());     // 编码
                mdVendor.setVendorName(mdVendorVO.getVendorName());     // 名称
                mdVendor.setEnableFlag(mdVendorVO.getEnableFlag());     // 是否启用
                mdVendor.setScheduleTime(nowTime);
                mdVendor.setCreateBy(mdVendorVO.getCreateBy());
                mdVendor.setCreateTime(mdVendorVO.getCreateTime());
                mdVendor.setUpdateBy(mdVendorVO.getUpdateBy());
                mdVendor.setUpdateTime(mdVendorVO.getUpdateTime());
                mdVendor.setErpVendId(mdVendorVO.getVendorId());
                insertList.add(mdVendor);
            }
            mdVendorService.saveBatch(insertList);
        }
        // 7找出erp和mes中都有的，这些需要更新
        ArrayList<Long> updateIdList = new ArrayList<>();
        for (int i = 0; i < mesIdList.size(); i++) {
            if (erpAllMdVendorIdList.contains(mesIdList.get(i))){
                updateIdList.add(mesIdList.get(i));
            }
        }
        if (updateIdList.size()>0) {
            ArrayList<MdVendor> mdVendorUpdateList = new ArrayList<>();
            List<MdVendor> mdVendorList = mdVendorMapper.selectBatchIds(updateIdList);
            for (MdVendor mdVendor : mdVendorList) {
                Long erpMdVendorId = mdVendor.getVendorId();
                MdVendorVO mdVendorVO = allMdVendorVOMap.get(erpMdVendorId);
                mdVendor.setVendorCode(mdVendorVO.getVendorCode());              // 编码
                mdVendor.setVendorName(mdVendorVO.getVendorName());              // 名称
                mdVendor.setEnableFlag(mdVendorVO.getEnableFlag());              // 生效状态

                mdVendor.setScheduleTime(nowTime);
                mdVendor.setCreateBy(mdVendorVO.getCreateBy());
                mdVendor.setCreateTime(mdVendorVO.getCreateTime());
                mdVendor.setUpdateBy(mdVendorVO.getUpdateBy());
                mdVendor.setUpdateTime(mdVendorVO.getUpdateTime());
                mdVendorUpdateList.add(mdVendor);
            }
            mdVendorService.updateBatchById(mdVendorUpdateList);
        }
    }
}
